package logic

import (
	"context"
	"crypto/rand"
	"encoding/hex"

	"greet/internal/svc"
	"greet/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type Chacha20Logic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChacha20Logic(ctx context.Context, svcCtx *svc.ServiceContext) *Chacha20Logic {
	return &Chacha20Logic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *Chacha20Logic) Chacha20(req *types.Chacha20Request) (resp *types.Chacha20Response, err error) {
	// Generate a 32-byte (256-bit) key
	key := make([]byte, 32)
	if _, err := rand.Read(key); err != nil {
		return &types.Chacha20Response{
			Code:    "error",
			Message: "Failed to generate key",
		}, nil
	}

	// Generate a 12-byte nonce
	nonce := make([]byte, 12)
	if _, err := rand.Read(nonce); err != nil {
		return &types.Chacha20Response{
			Code:    "error",
			Message: "Failed to generate nonce",
		}, nil
	}

	// Return the key and nonce as hex strings
	return &types.Chacha20Response{
		Code:    "success",
		Message: "Key and nonce generated successfully",
		Data: map[string]string{
			"key":   hex.EncodeToString(key),
			"nonce": hex.EncodeToString(nonce),
		},
	}, nil
}
